package com.tledu.spring05.aspect;

import com.tledu.spring05.annotation.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author Antg
 * @date 2021/8/9  19:25
 */
@Aspect
@Component
public class LogAspect {
    @Pointcut("@annotation(com.tledu.spring05.annotation.Log)")
    public void annotation() {

    }

    @Before("annotation()")
    public void before(JoinPoint joinPoint) {
        System.out.println("日志切面");
        //获取参数列表
        Class[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getMethod().getParameterTypes();
        Method method = null;
        try {
            method = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(), parameterTypes);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("类型-->" + method.getAnnotation(Log.class).type());
        System.out.println("描述-->" + method.getAnnotation(Log.class).desc());
    }


}
